package compute;

import java.util.ArrayList;

import data.DataBloc;
import data.Key;

public class TdeaEcb extends Mode {

	public TdeaEcb(KeyingOption op) {
		blocEncrypter = new TDEA(op);
	}

	public TdeaEcb(Key k1, Key k2, Key k3) {
		blocEncrypter = new TDEA(k1, k2, k3);
	}

	@Override
	public byte[] encrypt(String plainText) {
		ArrayList<DataBloc> plainTextDataBloc = DataBloc.toDataBloc(plainText);
		ArrayList<DataBloc> cypherTextDataBloc = new ArrayList<DataBloc>();
		for (DataBloc dataBloc : plainTextDataBloc) {
			cypherTextDataBloc.add(blocEncrypter.encrypt(dataBloc));
		}

		return DataBloc.toBytes(cypherTextDataBloc);
	}

	@Override
	public String decrypt(byte[] cyphetText) {
		ArrayList<DataBloc> cypherTextDataBloc = DataBloc
				.toDataBlocs(cyphetText);
		ArrayList<DataBloc> plainTextDataBloc = new ArrayList<DataBloc>();
		for (DataBloc dataBloc : cypherTextDataBloc) {
			plainTextDataBloc.add(blocEncrypter.decrypt(dataBloc));
		}
		return DataBloc.toString(plainTextDataBloc);
	}

	public Key getKey(int i) {
		return ((TDEA) blocEncrypter).getKey(i);
	}

	public void setKey(int i, Key k) {
		((TDEA) blocEncrypter).setKey(i, k);
	}

	public static void main(String[] args) {
		TdeaEcb tdeaEcb = new TdeaEcb(KeyingOption.KeyingOption1);

		String originalText = args[0];
		byte[] cypherText = tdeaEcb.encrypt(originalText);

		String plainText = tdeaEcb.decrypt(cypherText);

		System.out.println("Texte à crypter : " + originalText);

		System.out.print("Texte crypté : ");
		for (int i = 0; i < cypherText.length; i++) {
			System.out.print(cypherText[i] + " ");
		}

		System.out.println();
		System.out.println("Texte décrypté : " + plainText);
	}
}
